<?php

namespace App\Console\Commands\customer;

use App\model\customer\Customer;
use App\model\customer\CustomerSecondDepart;
use App\User;
use Illuminate\Console\Command;

class CustomerNumber extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:number';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        User::chunk(100,function ($users){
            foreach ($users as $user){
               $user->number= Customer::whereOwerUserId($user->id)->count();
               $this->info($user->number);
               $user->save();
            }
        });
        CustomerSecondDepart::all()->map(function (CustomerSecondDepart $customerSecondDepart){
            $customerSecondDepart->number=Customer::whereGroupId($customerSecondDepart->id)->count();
            $this->info( $customerSecondDepart->number);
            $customerSecondDepart->save();
        });
    }
}
